package com.huawei.esdk.tpoa.business.oa.convert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tpoa.business.oa.mapping.TPOADataMapping;
import com.huawei.esdk.tpoa.business.oa.sitemgr.TPOASiteManager;

public class TPOAConfMgrConvert 
{

    //把tp会议的数据转化为与之映射的exchange会议数据
	public Conference convertTPConf2ExchangeConf(Conference conf) 
	{
		Conference conference = new Conference(conf.getId());
		conference.setName(conf.getName());
		conference.setBeginTime(conf.getBeginTime());
		conference.setLength(conf.getLength());
		
		List<SiteInConference> sites = new ArrayList<SiteInConference>();
		
		for(SiteInConference site : conf.getSites())
		{
			String exchangeSiteAddress = TPOADataMapping.getRoomMailAddressBysiteUri(site.getUri());
        	if(null != exchangeSiteAddress)
        	{
        		SiteInConference siteInConference = new SiteInConference();
        		
				Site tpSite = TPOASiteManager.tpSites.get(site.getUri());
				siteInConference.setName(tpSite.getName());
				siteInConference.setUri(exchangeSiteAddress);
				
				sites.add(siteInConference);
        	}
		}
		
		conference.setSites(sites);
		
		return conference;
	}

    public void setInitialValueforSiteSync(Map<String, Conference> result, Map<String, String> confidmapping,
        Map<String, Conference> confListMap_before)
    {
        if(null == result)
        {
            return;
        }
        
      for(Entry<String, Conference> entry : result.entrySet())
      {
          String tpConfId = entry.getKey();
          Conference exchangeConfValue = entry.getValue();
          
          if(null != tpConfId && !tpConfId.isEmpty() &&
              null != exchangeConfValue)
          {
              confidmapping.put(tpConfId, exchangeConfValue.getId());
              confListMap_before.put(tpConfId, convertExchangeConf2TPConf(exchangeConfValue));
          }
      }
    }

    //把exchange会议的数据转化为与之映射的tp会议数据
    public Conference convertExchangeConf2TPConf(Conference conf) 
    {
        String tpConfId = TPOADataMapping.
            getTPConfIdByExchangeConfId(conf.getId());
        Conference conference = new Conference(tpConfId);
        conference.setName(conf.getName());
        conference.setBeginTime(conf.getBeginTime());
        conference.setLength(conf.getLength());
        
        List<SiteInConference> sites = new ArrayList<SiteInConference>();
        
        for(SiteInConference site : conf.getSites())
        {
            String tpSiteUri = TPOADataMapping.siteUriMapping.get(site.getUri());
            if(null != tpSiteUri)
            {
                SiteInConference siteInConference = new SiteInConference();

                siteInConference.setName(site.getName());
                siteInConference.setUri(tpSiteUri);
                
                sites.add(siteInConference);
            }
        }
        
        conference.setSites(sites);
        
        return conference;
    }
    
}
